
#include "SDK30.H"
#include "_emvdctag_.h"
#include "_emvdc_.h"
#include "del_lib.h"		

#include "servcust.h"

#include <fm_interApp.h>
#include <fm_proto.h>
#include <fm_HMIService.h>
#include <fm_genpinpad.h>

#define __ENTER_KEY		-1
#define __BACK_KEY		-2
#define __EXIT_KEY		-3
#define NUMBER_OF_ITEMS(a)			(sizeof(a)/sizeof((a)[0]))

//static int DisplayHeaderStatus;
DATE g_date;

void CallApp(unsigned short AppNo, unsigned short evtid);
char * dpmThisApplicationName(void);
extern int PasswordEntry(char *pwd,int line, int col,int nMinLen,
					   int nMaxLen, int instar, int nTimeout );

ST_appreg totapreg;

unsigned short appid_act;

const unsigned char kartuku[]=
{
0, 128,
0x00, 
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x81, 0x00, 0x00, 0x00, 0x00, 
0x3E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x1F, 0x8F, 0x8F, 0xC7, 0xC7, 0xC7, 0xC7, 0x87, 
0x87, 0x07, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xFF, 
0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x0F, 0x1F, 0x3F, 
0xFF, 0xFE, 0xFC, 0xFC, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 
0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 0xFC, 0xF8, 0xE0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xFF,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, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x01, 0x03, 0x07, 0x1F, 0x3F, 0x3F, 0x7F, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00, 0x80, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x80, 0x00, 
128, 0
};

const char *MasterMenu[] =
{
	"About",
};


static const unsigned char appname[]    = "MASTER";

static uint8 animFlag;

StructListe Menu;

/*void RunEvent4App(e_eventtype eventid)
{
	int i, j;
	

	for (i=0;i<totapreg.totapp;i++)
	{
		for (j=0;j<50;j++)		
			if (totapreg.stap[i].evtsupport[j]==eventid)
				CallApp(totapreg.stap[i].apptype,eventid);		
	}

}*/

void xx_print(char buffer)
{
FILE *prn;
	prn=fopen("PRINTER","w-");
		fprintf(prn,buffer);
	fclose(prn);
}
void InitDefaut(int flag, int offset)
{
    int MAXX;
    int MAXY;

    GetScreenSize(&MAXY, &MAXX);
    Menu.Fenetre.left = 0; /* */
    Menu.Fenetre.top = 0; /* */
    Menu.Fenetre.rigth = (MAXX - 1); /* */
    Menu.Fenetre.bottom = (MAXY - 1); /* */
    Menu.Fenetre.nblines = 4; /* */
    Menu.Fenetre.fontsize = _MEDIUM_; /* */
    Menu.Fenetre.type = _PROPORTIONNEL_; /* */
    Menu.Fenetre.police = 0; /* */
    Menu.Fenetre.correct = flag; /* */
    Menu.Fenetre.offset = offset; /* */
    Menu.Fenetre.shortcommand = _ON_; /* */
    Menu.Fenetre.selected = _OFF_; /* */
    Menu.Fenetre.thickness = 2; /* */
    Menu.Fenetre.border = _ON_; /* */
    Menu.Fenetre.popup = _NOPOPUP_; /* */
    Menu.Fenetre.first = 0; /* */
    Menu.Fenetre.current = 0; /* */
    Menu.Fenetre.time_out = 120;
}

unsigned short GetAppID(e_eventtype eventid)
{ 
	int i, j, k;
	unsigned char cr;
	unsigned short ind[MAX_APP];
	
	BUFFER_SAISIE buf_saisie;
	k=0;

	InitDefaut(_ON_, 1);
	memset(&ind,0x00, sizeof(ind));
	for (i=0;i<totapreg.totapp;i++)
	{
		for (j=0;j<50;j++)
		{
			if (totapreg.stap[i].evtsupport[j]==eventid)
			{	
				ind[k]=totapreg.stap[i].apptype;
				Menu.tab[k++]=totapreg.stap[i].appname;				
			}
		}
	}

	StateHeaders(0);
	Menu.Fenetre.titre = "Choose Application\n";
    	Menu.tab[k] = NULL;
		
	if (k==1) //only one application which support the Event		
		return ind[0];

	
    	G_List_Entry(&Menu);
    	ttestall(ENTRY, 0);
    	cr = Read_Saisie(&buf_saisie);
    	//StateHeader(1);
		
    	if (cr == CR_ENTRY_OK)
    	{
        	return  ind[buf_saisie.donnees[0]];
    	}
	
	return 0;
    
}

int is_for_you_before(NO_SEGMENT noappli, S_TRANSIN *param_in, S_ETATOUT    *param_out)
{
	    S_ETATOUT etatout;

   
		_trace("	is_for_you_before");
	    if(param_in->support == CHIP_SUPPORT)	    
	    	{
	    		read_date(&g_date);
			appid_act=GetAppID(EVMSG_EMV_PROC);			
			if (!appid_act)
				return STOP;		
	    	}
		else if (			
			(param_in->support == TRACK2_SUPPORT) ||
			(param_in->support ==TRACK12_SUPPORT) ||
			(param_in->support ==TRACK32_SUPPORT) ||
			(param_in->support ==TRACK123_SUPPORT)
		)
		{
//			_trace("	Magentic");
			appid_act=GetAppID(EVMSG_ASK_MAG_CARD);			
			if (!appid_act)
			{
//				_trace("	No Magentic");
				return STOP;	
			}
	
		}
		memcpy (&etatout, param_out, sizeof(etatout));
		// give my application name 
		strcpy((char *)etatout.returned_state[etatout.response_number].appname,(char *)appname);
		// accept this card 
		etatout.returned_state[etatout.response_number].state.response = 2;
		// give my application number
		etatout.returned_state[etatout.response_number].no_appli = noappli;
		// increment the response number 
		etatout.response_number++;
		memcpy (param_out, &etatout, sizeof(etatout));
	/*
	    memcpy(&etatout, param_out, sizeof(etatout));
		
	    etatout.returned_state[etatout.response_number].state.response = 0;
	    memcpy(param_out, &etatout, sizeof(etatout));
	*/
	    return  FCT_OK;//is_name(noappli, PT_NULL, param_out);
}

int Main_EMVCUST(unsigned int size, StructProcessStep *data)
{
 	unsigned char priority;
	unsigned int  ret_code;
	int DisplayHeaderStatus;
	
	
	
	if (!appid_act)
		appid_act=DEFAULT_MAG_APP;
	
	
	
	if (ServiceGet(appid_act,data->service,&priority)==0)	
	{
		
		//DisplayHeaderStatus=StateHeader(0); // disable display header	
		USR_ServiceCall(appid_act,data->service,sizeof(StructProcessStep),data,&ret_code);	
		//StateHeader(DisplayHeaderStatus);
		return ret_code;
	}
	
  	return (FCT_OK);
}


 int CallClientEvent(unsigned short AppNo, ST_InterApp* _stinter)
 {
	unsigned char priority;
	unsigned int  ret_code=0;
	int DisplayHeaderStatus;
	byte *buff;
		
	if (!AppNo)
		AppNo=DEFAULT_MAG_APP;


	
	DisplayHeaderStatus=StateHeader(0);
	pp_display_clear(0);
	if (ServiceGet(AppNo,SERVICE_EVENTMASTER_CALL,&priority)==0)	
	{
		
		buff=umalloc(sizeof(ST_InterApp)+10);
		memcpy(buff,_stinter,sizeof(ST_InterApp));		
		USR_ServiceCall(AppNo,SERVICE_EVENTMASTER_CALL,sizeof(ST_InterApp),buff, &ret_code);
		memcpy(_stinter,buff,sizeof(ST_InterApp));
		ufree(buff);
	}
	
	StateHeader(DisplayHeaderStatus);


	return ret_code;
 }


int ManageMenu( const char *szTitle, int bRadioButtons, int nDefaultChoice, 
				int nItems, const char* Items[] )
{
	FILE *hDisplay;
	int DisplayHeaderStatuss;

	// Menu.
	StructList Menu;
	int nY;
	int nMaxX;
	int nMaxY;

	ENTRY_BUFFER Entry;

	//
	int i;
	int nInput;
	int nReturn;
	
	//
	hDisplay = fopen( "DISPLAY", "w" );

	// Get Screen size.
	GetScreenSize( &nMaxY, &nMaxX );

	// For the menu height of the menu,
	nY = 0;

	DisplayHeaderStatuss=StateHeader(0); // disable display header

	if ((nDefaultChoice < 0) || (nDefaultChoice >= nItems))
	{
		nDefaultChoice = 0;
	}

	//
	CreateGraphics(_MEDIUM_);

	//
	memset( &Menu, 0, sizeof(Menu) );
	Menu.MyWindow.left   = 0;
	Menu.MyWindow.top    = nY;
	Menu.MyWindow.rigth  = nMaxX - 1;
	Menu.MyWindow.bottom = nMaxY - 1;
	if( nMaxY == 128 )
	{
		Menu.MyWindow.nblines = 10;
	}
	else
	{
		Menu.MyWindow.nblines = 5;
	}

	Menu.MyWindow.fontsize		= _MEDIUM_;
	Menu.MyWindow.type			= _PROPORTIONNEL_;
	Menu.MyWindow.font			= 0;
	Menu.MyWindow.correct			= _ON_;
	Menu.MyWindow.offset			= 0;
	Menu.MyWindow.shortcommand	= _ON_;
	if( bRadioButtons )
	{
		Menu.MyWindow.selected = _ON_;
	}
	else
	{
		Menu.MyWindow.selected = _OFF_;
	}

	Menu.MyWindow.thickness		= 2;
	Menu.MyWindow.border		= _ON_;
	Menu.MyWindow.popup			= _NOPOPUP_;
	Menu.MyWindow.first			= nDefaultChoice;
	Menu.MyWindow.current		= nDefaultChoice;
	Menu.MyWindow.time_out		= 60;
	Menu.MyWindow.title			= (unsigned char*)szTitle;

	for( i = 0; i < nItems; i++ )
	{
		Menu.tab[i] = (unsigned char*)Items[i];
	}

	G_List_Entry((void*)&Menu);
	ttestall(ENTRY, 0);
	nInput = Get_Entry((void*)&Entry);

	switch( nInput )
	{
	case CR_ENTRY_OK:
		nReturn = Entry.d_entry[0];
		break;

	case CR_ENTRY_NOK:
		nReturn = __EXIT_KEY;
		break;

	default:
		nReturn = __BACK_KEY;
		break;
	}
	StateHeader(DisplayHeaderStatuss); // move display header in previous state
	fclose( hDisplay );
	
	return nReturn;
}


int more_function( NO_SEGMENT no, void *p1, void *p2 )
{

	int bContinue = 1;
	do
	{
		switch( ManageMenu( "MASTER", 0, 0, NUMBER_OF_ITEMS(MasterMenu), MasterMenu ))
		{
		case 0:
			pp_open_hmi();
			StateHeader(0);
			pp_display_clear(0);
			pp_display_text4("ABOUT MASTER", 
				NULL, 0, 
				dpmThisApplicationName(), PT_ALIGN_LEFT | PT_CLEAR, 
				"EMV MASTER", PT_ALIGN_LEFT | PT_CLEAR, 
				//"v1.0.1 D20080811", PT_ALIGN_LEFT | PT_CLEAR);
				"v2.1.0 D20081028", PT_ALIGN_LEFT | PT_CLEAR);
			pp_ttestall(KEYBOARD, 15*100);
			StateHeader(1);
			pp_close_hmi();
			bContinue = 0;
			break;
/*
		case 1:		
			break;

		case 2:		
			break;
*/
		default:
			bContinue = 0;
			break;
		}
	}
	while( bContinue == 1 );

	return FCT_OK;
}


int after_reset( NO_SEGMENT no, void *p1, S_TRANSOUT *etatseq )
{		
	animFlag = 0;	
	return FCT_OK;
}

int is_state(NO_SEGMENT no,void *p1,S_ETATOUT *etatseq)
{
	S_ETATOUT etatout;
	int retour; 
	
	memcpy (&etatout, etatseq, sizeof(etatout));
	etatout.returned_state[etatout.response_number].state.response = REP_OK;
	memcpy (etatseq, &etatout, sizeof(etatout));
	retour = is_name (no, PT_NULL, etatseq);
	return (retour);
}

int is_name(NO_SEGMENT no,void *p1,S_ETATOUT *etatseq)
{
	S_ETATOUT etatout;
	
	memcpy ((char *)&etatout, (char *)etatseq, sizeof(etatout));
	strcpy((char *)etatout.returned_state[etatout.response_number].appname,(char *)appname);
	etatout.returned_state[etatout.response_number].no_appli = no;
	etatout.response_number++;
	memcpy (etatseq, &etatout, sizeof(etatout));
	return (FCT_OK);
}

int give_your_domain(NO_SEGMENT no,void *p1,S_INITPARAMOUT *param_out)
{
	S_INITPARAMOUT etatout;
	
	memcpy (&etatout, param_out, sizeof(etatout));
	etatout.returned_state[etatout.response_number].mask     = MSK_MDP|MSK_SWIPE|MSK_TYPE_PPAD|MSK_PINPAD|MSK_STANDARD|MSK_LANGUE|MSK_FRMT_DATE|MSK_DATE;
	etatout.returned_state[etatout.response_number].application_type = TYP_EXPORT;
	etatout.response_number++;
	memcpy (param_out, &etatout, sizeof(etatout));
	return (FCT_OK);
}

int is_delete(NO_SEGMENT no,void *paramin,S_DELETE *paramout)
{
	paramout->deleting=DEL_YES;
	return (FCT_OK);
}

int state (NO_SEGMENT noappli,void *p1,void *p2)
{
	return FCT_OK;
}


int mcall (NO_SEGMENT noappli,void *p1,void *p2)
{
	return FCT_OK;
}

int consult (NO_SEGMENT noappli,void *p1,void *p2)
{
	return FCT_OK;
}

int time_function (NO_SEGMENT noappli,void *p1,void *p2)
{	
	return (FCT_OK);
}

int keyboard_event(NO_SEGMENT noappli,S_KEY *p1,S_KEY *p2)
{
	S_KEY keyA, keyB;
	ST_InterApp st_it;		
	int DisplayHeaderStatus;
//	int ind=-1;
	memcpy(&keyA,p1,sizeof(keyA));
	memcpy(&keyB,p2,sizeof(keyB));

	pp_set_frame(FALSE);
	switch (keyA.keycode)
	{
	case N0:
	case N1: 
	case N2:
	case N3: 
	case N4: 
	case N5: 
	case N6: 
	case N7: 
	case N8: 
	case N9: 
	case T_VAL :
		keyB.keycode = keyA.keycode;
		keyB.keycode = 0;
		//keyA.keycode = 0;
		break;
	case T_POINT : 
		// inhibits these keys for International domain
		       appid_act=GetAppID(EVMSG_FUNCTION);			
			if (appid_act)
			{				
				st_it.serviceid= EVMSG_FUNCTION;
				st_it.no=0;				
				
				CallClientEvent(appid_act,&st_it);						
			}
		keyB.keycode = 0 ; 
		break; 

	case F1:
	case F2:
	case F3:
	case F4:
		// inhibits these keys for International domain
		       appid_act=GetAppID(EVMSG_IDLE_APP);			
			if (appid_act)
			{				
				st_it.serviceid= EVMSG_IDLE_APP;
				st_it.no=keyA.keycode;				
				
				CallClientEvent(appid_act,&st_it);						
			}
		keyB.keycode = 0 ; 
		break; 
/*		
	case F1 : 	
	case F2 : 
	case F3 : 
	case F4 : 
		{
			switch(keyA.keycode)
			{
			case F1:
				ind=0;break;
			case F2:
				ind=1;break;
			case F3:
				ind=2;break;	
			case F4:
				ind=4;break;
			}
			
			if (ind<totapreg.totapp)
			{				
				ST_InterApp st_it;		
				
				st_it.serviceid=EVMSG_IDLE_APP;
				st_it.no=0;
				
				_trace("EVMSG_IDLE_APP");				
				CallClientEvent(totapreg.stap[ind].apptype,&st_it);
				appid_act=totapreg.stap[ind].apptype;
				keyB.keycode =NAVI_CLEAR;
				_trace("EVMSG_IDLE_APP SUCCESS");
			}		
				
		}
		break;
*/

//aan, 11 08 2008
	case UP:
//	case DOWN:
		// inhibits these keys for International domain
		       appid_act=GetAppID(EVMSG_IDLE_APP);			
			if (appid_act)
			{				
				st_it.serviceid= EVMSG_IDLE_APP;
				st_it.no=keyA.keycode;				
				
				CallClientEvent(appid_act,&st_it);						
			}
		keyB.keycode = 0 ; 
		break;
//end of aan
	case T_CORR :
	case T_ANN : 
	case NAVI_CLEAR : 
	case NAVI_OK : 
//	case UP : 
	case DOWN : // return the same key value for keys above ! 
	case T_F :  // do not filter F key and return the same key ! 

		if (keyA.keycode==T_F)
		{
			char pwdBuf[6];
			memset(pwdBuf, 0, sizeof(pwdBuf));
			pp_open_hmi();
			DisplayHeaderStatus=StateHeader(0);

			pp_display_clear(0);
			pp_display_text(2, 0, "SECURITY CODE ?", PT_ALIGN_LEFT | PT_CLEAR);
			//if(IsPasswordOK(prtSecurityCode, PASS_SECURITY_CODE, 6) != SUCCESS)
			if(PasswordEntry(pwdBuf, 3, 10, 6, 6, TRUE, 30)==0)
				keyA.keycode=NAVI_CLEAR;
			else
				{
				if(memcmp(pwdBuf,"328926", 6)!=0)
					{
					pp_beep(3);
					pp_display_text(3, 0, "INVALID CODE", PT_ALIGN_CENTER | PT_CLEAR);
					keyA.keycode=NAVI_CLEAR;	
					}
				}
			StateHeader(DisplayHeaderStatus);
			
			pp_close_hmi();
		}
	
		keyB.keycode = keyA.keycode;  
		break; 
	}
	memcpy(p2,&keyB,sizeof(keyB));
	return (FCT_OK);
}


int file_received(NO_SEGMENT no,S_FILE * param_in,void *p2)
{

	S_FS_PARAM_CREATE ParamCreat;
	int		 Retour;
	char		 Dir_File[25];
	char		 Dir_Label[25];
	S_FS_FILE      *pFile;
	
	memclr(Dir_File,sizeof(Dir_File));
	memclr(Dir_Label,sizeof(Dir_Label));
	
	sprintf(Dir_Label,"/%s",param_in->volume_name);
	ParamCreat.Mode = FS_WRITEONCE;
	Retour = FS_mount (Dir_Label,&ParamCreat.Mode);
	if (Retour == FS_OK)
	{
		sprintf(Dir_File,"/%s/%s",param_in->volume_name,param_in->file_name);
		// the file can be open at this stage
		pFile = FS_open (Dir_File, "r");
		// read the file and get parameters 
		FS_close(pFile);
		// cannot be deleted as it is located in system disk 
		FS_unmount(Dir_Label);
	}

	return (FCT_OK);
}



int is_evol_pg(NO_SEGMENT noappli, void *p1, S_ETATOUT *param_out)
{
	S_ETATOUT etatout;
	int       retour;
	memcpy(&etatout, param_out, sizeof(etatout));
	etatout.returned_state[etatout.response_number].state.response=REP_OK;
	memcpy(param_out,&etatout,sizeof(etatout));
	retour = is_name (noappli, PT_NULL, param_out);
	return(FCT_OK);		 
}

int is_time_function(NO_SEGMENT noappli, void *p1, S_ETATOUT *param_out)
{
	S_ETATOUT etatout;
	int       retour;
	memcpy(&etatout, param_out, sizeof(etatout));
	etatout.returned_state[etatout.response_number].state.response=REP_OK;
	memcpy(param_out,&etatout,sizeof(etatout));
	retour = is_name (noappli, PT_NULL, param_out);
	return(FCT_OK);		 
}


int is_change_init(NO_SEGMENT noappli, void *p1, S_ETATOUT *param_out)
{
	S_ETATOUT etatout;
	int       retour;
	memcpy(&etatout, param_out, sizeof(etatout));
	// accept all 
	etatout.returned_state[etatout.response_number].state.mask=0;
	memcpy(param_out,&etatout,sizeof(etatout));
	retour = is_name (noappli, PT_NULL, param_out);
	return(FCT_OK);		 
}

int modif_param(NO_SEGMENT noappli, S_MODIF_P *param_in, void *p2)
{
	S_MODIF_P param_changed;
	
	memcpy(&param_changed, param_in,sizeof(param_changed));	
	return(FCT_OK);
}



int is_for_you_after(NO_SEGMENT no, S_TRANSIN *param_in, S_CARDOUT *param_out)
{
	S_CARDOUT card_out;
    	S_ETATOUT etatout;

//	 _trace("is_for_you_after");		
	    etatout.response_number = 0;
	    is_name(no, NULL, &etatout);

	   
	    /* Card recognised only when mag stripe is used       
	    */
	    memcpy(&card_out, param_out, sizeof(card_out));
	    if ((param_in->support == TRACK2_SUPPORT) || (param_in->support ==
	        TRACK12_SUPPORT) || (param_in->support == TRACK32_SUPPORT))
	    {
	    	strcpy(card_out.returned_state[card_out.response_number].appname,
            (char*)etatout.returned_state[0].appname);
        	card_out.returned_state[card_out.response_number].no_appli = no;
        	card_out.returned_state[card_out.response_number].cardappnumber = 1;
        	card_out.returned_state[card_out.response_number].cardapp[0].priority =
            		10; //30;
        	strcpy(card_out.returned_state[card_out.response_number].cardapp[0].cardappname, (char*)etatout.returned_state[0].appname);
        card_out.response_number++;
		
	     	//memcpy(&card_out, param_out, sizeof(card_out));		
	        //card_out.returned_state[card_out.response_number].cardappnumber = 0;
	            //refuse to process the card
	        //card_out.returned_state[card_out.response_number].cardapp[0].priority =  0;
		 //memcpy(param_out, &card_out, sizeof(card_out));		
//		  _trace("is_for_you_after Mag");	
	    }
	    else
	    {
	        /* This is a Chip card            */	
	      	int ret_code=FCT_OK;			
		ST_InterApp st_it;

// select EMV Application
		_trace("	is_for_you_after");
#if 0
		appid_act=GetAppID(EVMSG_ISFORYOUAFTER);			
		if (!appid_act)
		{
			return STOP;	
		}
#endif
		st_it.serviceid=EVMSG_ISFORYOUAFTER;
		st_it.no=no;
		
		memcpy(&(st_it.par_in.in_for_you_after), param_in, sizeof(S_TRANSIN));	
		ret_code= CallClientEvent(appid_act,&st_it); 				
		memcpy(param_out, &(st_it.par_out.out_for_you_after), sizeof(S_CARDOUT));
		
		return ret_code;
	    }
	   

	 return (FCT_OK);

}



int debit_non_emv (NO_SEGMENT no, S_TRANSIN * param_in, S_TRANSOUT * param_out)
{
	int len,i, ret_code;	
	ST_InterApp st_it;
	
	st_it.par_out.out_debitnonemv=FALSE;
	st_it.ins_date=g_date;
//	_trace("debit_non_emv");
	switch(param_in->support)
	{
	case CHIP_SUPPORT ://3				
		break;
	case TRACK12_SUPPORT:	//1
	case TRACK32_SUPPORT:	//2
	case TRACK2_SUPPORT:	//0
	case TRACK123_SUPPORT:	//6	
		
		len=strlen(param_in->track2);
		for (i=0;i<len;i++)
		{
			if (param_in->track2[i]=='D')
				param_in->track2[i]='=';
		}
		

		st_it.serviceid=EVMSG_ASK_MAG_CARD;
		st_it.no=no;	
		memcpy(&(st_it.par_in.in_debitnonemv), param_in, sizeof(S_TRANSIN));	
		ret_code= CallClientEvent(appid_act,&st_it); 				
		break;
	case OPERATOR_SUPPORT ://4
	case UNKNOWN_SUPPORT:
		break;
	}

	//pp_close_hmi();
	param_out->noappli      = no;
	param_out->rc_payment  = PAY_OK;
	return (FCT_OK);
}


int select_aid_extended(NO_SEGMENT noappli, S_SELECT_AID_EXTENDED *param_in,  S_AID_OUT *param_out)
{  		
	int ret_code;
	ST_InterApp st_it;

	st_it.serviceid=EVMSG_EMV_SELECT_AID_EXT;
	st_it.no=noappli;
	
	memcpy(&st_it.par_in.in_select_aid_extended, param_in, sizeof(S_SELECT_AID_EXTENDED));	
	ret_code= CallClientEvent(appid_act,&st_it); 
	memcpy(param_out, &st_it.par_out.out_select_aid_extended, sizeof(S_AID_OUT));
	return ret_code;
}

int select_aid(NO_SEGMENT no, S_SELECT_AID *param_in, S_AID_OUT *param_out)
{
	int ret_code;
	ST_InterApp st_it;

	st_it.serviceid=EVMSG_EMV_SELECT_AID;
	st_it.no=no;
	
	memcpy(&(st_it.par_in.in_select_aid), param_in, sizeof(S_SELECT_AID));	
	ret_code= CallClientEvent(appid_act,&st_it); 
	memcpy(param_out, &(st_it.par_out.out_select_aid_extended), sizeof(S_AID_OUT));
	return ret_code;
	
	//return FCT_OK;//CallClientEvent(appid_act,EVMSG_EMV_SELECT_AID,param_in,param_out, no);	
}

int fall_back(NO_SEGMENT no, S_TRANSIN *param_in,   void* param_out)
{
	int ret_code;
	ST_InterApp st_it;

	st_it.serviceid=EVMSG_FALLBACK;
	st_it.no=no;
	
	memcpy(&(st_it.par_in.in_fall_back), param_in, sizeof(S_TRANSIN));	
	ret_code= CallClientEvent(appid_act,&st_it); 
	memcpy(param_out, &(st_it.par_out.out_select_aid_extended), sizeof(S_AID_OUT));
	return ret_code;
	//return FCT_OK;//CallClientEvent(appid_act,EVMSG_FALLBACK,param_in,param_out,no);
}


void AnimSwipe(void)
{
	FILE *fl;
	int fnt;

	char tmp[50];
	uint8 xs, ys;

	uint8 l;
	DATE tmpDate;
	
	fl=fopen("DISPLAY","w");	

	_clrscr();
	PaintGraphics();
	
	if(pp_get_hardware_type() == TYPE_TERMINAL_SMART)// nazar, add on EFTSMART
		{
			read_date(&tmpDate);
		}

	
	fnt=GetDefaultFont();
	_clrscr();
	SetType(_PROPORTIONNEL_);	

	if(pp_get_hardware_type() == TYPE_TERMINAL_SMART)	// nazar, add on EFTSMART
	{
		//date
		memset(tmp,' ',sizeof(tmp));
		memcpy(tmp, tmpDate.day, 2);
		tmp[2]=tmp[5]='/';
		memcpy(&tmp[3], tmpDate.month, 2);
		memcpy(&tmp[6], "20", 2);
		memcpy(&tmp[8], tmpDate.year, 2);
		//time
		memcpy(&tmp[11], tmpDate.hour, 2);
		tmp[13]=':';
		memcpy(&tmp[14], tmpDate.minute, 2);
		tmp[16]='\0';
		SetArea(0, 0, 128, 23,_OFF_);
		DrawString(tmp, 0, 0, _OFF_);
	}

	
	//PaintGraphics();
	//SetType(_PROPORTIONNEL_);	
	//SetType(_NORMALE_);	
	if(pp_get_hardware_type() == TYPE_TERMINAL_SMART)	// nazar, add on EFTSMART	
		{
			SetBitmap(0, 20, (unsigned char*)kartuku,_OFF_);
		}
	else
		{	// assume it's 930G
			SetBitmap(0, 23, (unsigned char*)kartuku,_OFF_);
		}
	SetArea(0, 52, 128, 60,_OFF_);
	//SetArea(0, 52, 128, 60,_ON_);
	memset(tmp,' ',sizeof(tmp));
	tmp[50]='\0';
	xs = 0;
	ys = 54;// 52
	switch(animFlag)
		{
		case 0:
			strcpy(tmp, "SWIPE CARD              ");
			xs = 35;
			animFlag++;
			break;
		case 1:
			strcpy(tmp, "F1 : MORE MENU          ");
			xs = 26;// 30;
			animFlag++;
			break;
		case 2:
			strcpy(tmp, "F4: SETTLE              ");
			xs = 35;
			animFlag++;
			break;
		case 3:
			strcpy(tmp, "F2: REPRINT LAST        ");
			xs = 21;// 25;
			animFlag++;
			break;
		case 4:
			strcpy(tmp, "F3: REPRINT  ANY        ");
			xs = 21;// 25;
			animFlag=0;
			break;
		}
	//DrawString(tmp, xs,ys,_OFF_);  
	DrawString(tmp, xs, ys, _OFF_);
//	DrawExtendedString(xs, ys, tmp, _OFF_, _POLICE5x7_, _NORMALE_);
		
	PaintGraphics();			

	//SetFont(fnt);
	SetDefaultFont(fnt);
	fclose(fl);
	
}


int idle_message (NO_SEGMENT no,void *p1,void *p2)
{
/*
	FILE *hDisplay;
	int nFont;
	int i,y,k;
	char tmp[100];
	
	// Open the display driver.
	hDisplay = fopen("DISPLAY","w");
	
	nFont = GetDefaultFont();

	//StateHeader(1);//DisplayHeaderStatus);
	//CreateGraphics(_MEDIUM_);	
	_clrscr();
	PaintGraphics();//disable
//	SetBitmap(0, 23, (unsigned char*)kartuku,_OFF_);
//	SetArea(0, 52, 128, 65,_OFF_);
	
	
	SetType(_PROPORTIONNEL_);//disable
	CreateGraphics(_MEDIUM_);//disable
	
	for (i=0;i<totapreg.totapp;i++)
	{
		if (i<3)
		{
			sprintf(tmp,"%d-%s",i+1, totapreg.stap[i].appname);
			DrawString(tmp,1,23+(i*12),_OFF_);  	  			
		}
		else
		{
			y=i-3;		
			sprintf(tmp,"%s-%d", totapreg.stap[i].appname,i+1);
			k=127-StringWidth(tmp);
			DrawString(tmp,k,23+(y*12),_OFF_);  
		}
	}
	
//	DrawExtendedString(15, 54, "SWIPE/INSERT CARD", _OFF_, _POLICE5x7_, _NORMALE_);
	PaintGraphics();		

	SetDefaultFont( nFont );
	fclose( hDisplay );
*/
	
	_clrscr();
	PaintGraphics();		
	
	AnimSwipe();
	return (FCT_OK);
}

byte checkapp(unsigned short apptype)
{
	int i;
	for (i=0;i<totapreg.totapp;i++)
	{
		if (totapreg.stap[i].apptype==apptype)
			return TRUE;
	}
	
	return FALSE;	
}


int GetApplist(int size, ST_applist *Data)
{
	int t;
	byte k;
	
	k=checkapp(Data->apptype);	
	Data->retval=FALSE;	
	if (!k)
	{
		t=totapreg.totapp;
		memcpy(&totapreg.stap[t], Data, sizeof(ST_applist));		
		totapreg.totapp++;
		Data->retval=TRUE;	
	}
	else
		Data->retval=FALSE;
			
	return FCT_OK;
}


typedef struct
{
    	unsigned short type_code;
	unsigned short service;
	unsigned long m_ulAmount;
	int m_nCurrencyCode;
} T_PERFORM_PAYMENT;

int PerformPayment(unsigned int size, T_PERFORM_PAYMENT* pPaymentData)
{	
	unsigned char priority;
	unsigned int  ret_code;
	int DisplayHeaderStatus;
	
	if (!appid_act)
		return (FCT_OK);
	 //disable display header	
	if (ServiceGet(appid_act,I_EMVCUST_Perform_Transaction,&priority)==0)	
	{
		DisplayHeaderStatus=StateHeader(0);
		USR_ServiceCall(appid_act, I_EMVCUST_Perform_Transaction, sizeof(T_PERFORM_PAYMENT), (void*)pPaymentData,&ret_code);	
		StateHeader(DisplayHeaderStatus);
		return ret_code;
	}

  	return (FCT_OK);	
}

typedef int (*T_SERVICE_FUNCTION)(unsigned int nSize, void*Data);

int give_interface(unsigned short no,void *p1,void *p2)
{

 service_desc_t MesServices[]=
	{
		{no	,IS_NAME						,(SAP)Main						,10},
		{no	,MORE_FUNCTION					,(SAP)Main						,10},
		{no	,IDLE_MESSAGE					,(SAP)Main						,70},
		{no	,KEYBOARD_EVENT				,(SAP)Main						,30},
		{no	,AFTER_RESET					,(SAP)Main						,30},
		{no	,CONSULT						,(SAP)Main						,30},
		{no	,STATE							,(SAP)Main						,30},
		{no	,IS_CHANGE_INIT					,(SAP)Main						,30},
		{no	,IS_EVOL_PG						,(SAP)Main						,30},
		{no	,IS_STATE						,(SAP)Main						,30},
		{no	,IS_TIME_FUNCTION				,(SAP)Main						,30},
		{no	,MCALL							,(SAP)Main						,30},
		{no	,MODIF_PARAM					,(SAP)Main						,30},
		{no	,IS_FOR_YOU_BEFORE				,(SAP)Main						,30},
		{no	,DEBIT_NON_EMV					,(SAP)Main						,30},
		{no	,FILE_RECEIVED					,(SAP)Main						,30},
		{no	,GIVE_YOUR_DOMAIN				,(SAP)Main						,30},
		{no	,IS_FOR_YOU_AFTER				,(SAP)Main						,30},
		{no	,IS_DELETE 						,(SAP)Main						,30},
		{no	,FALL_BACK 						,(SAP)Main						,30},

		{no	,SELECT_AID_EXTENDED 			,(SAP)Main						,30},
		{no	,TRACK_STREAM 					,(SAP)Main						,30},
		{no	,SELECT_AID 						,(SAP)Main						,30},		
		{no	,SERVICE_APPREG_CALL			,(SAP)GetApplist					,10},
		{no	,SERVICE_HMI_CALL				,(SAP)Service_HMI_call			,10},		
		{no	,I_EMVCUST_Perform_Transaction	,(SAP)PerformPayment			,30},
		
		{no	,I_EMVCUST_Initialise 				,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Get_Global_Param 		,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Get_AID_List 			,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Get_AID_DATA 		,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Get_AID_Param 		,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_More_Function 			,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Periodic_Call 			,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Process_Step 			,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Display_Message 		,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Get_Last_Transaction	,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Voice_Referral 			,(SAP)Main_EMVCUST				,30},
		{no	,I_EMVCUST_Get_AID 				,(SAP)Main_EMVCUST				,30},
	#ifndef _FULLEMV_	
		{no	,I_EMVCUST_KeyManagement 		,(SAP) Main_EMVCUST				,30},
	#endif	
		{no	,I_EMVCUST_Authorisation 			,(SAP) Main_EMVCUST				,30},
		{no	,I_EMVCUST_Choose_Language_Menu ,(SAP) Main_EMVCUST				,30},
		{no	,I_EMVCUST_Menu_Select 			,(SAP) Main_EMVCUST				,30},		
		{no	,I_EMVCUST_Choose_Account_Type_Menu 	, (SAP) Main_EMVCUST		,30},
		{no	,SERVICE_HMI_CALL 				, (SAP) Service_HMI_call			,30}	
	};
	
	
	int 	i = sizeof(MesServices)/sizeof(service_desc_t);			

	appid_act=0;
	ServiceRegister(i,MesServices);

	return FCT_OK;
}

void entry(void)
{
	object_info_t info;
	
	
	ObjectGetInfo(OBJECT_TYPE_APPLI, ApplicationGetCurrent(),&info);
	
	give_interface(info.application_type, NULL, NULL);

	// Open all the external libraries.

	paramlib_open();	// TELIUM Manager Library 
	extenslib_open();	// Extension Library 
	svlib_open();		// SV Library (for date management).
	libgrlib_open();	// Graphic Library 
	entrylib_open();	//
	#ifndef _SIMULPC_
    PPS_Dll_init();
    #endif
}

//aan
char * dpmThisApplicationName(void)
{
	static	char 	AppliName[30];
	int rc,on;
	object_info_t info;

//	AppliName[0] = NUL;
	memset(AppliName, 0x00, sizeof(AppliName));

	on=ObjectGetNumber(OBJECT_TYPE_APPLI);
	//rc=ObjectGetInfo( OBJECT_TYPE_APPLI,0,&info);
	rc=ObjectGetInfo( OBJECT_TYPE_APPLI,(unsigned int)ApplicationGetCurrent(),&info);

	if (rc)
		return NULL;
	
	memcpy(AppliName,info.name,OBJECT_NAME_LEN);
	//strcat(AppliName,"-");
	//strcat(AppliName,info.file_name);
	AppliName[16] = '\0';;

	return AppliName;
}



